# -*- coding:utf-8 -*-
# @File       :   conftest.py
# @Author     :   LINING
# @Time       :   2024/7/22 16:02
# @Description :  用例执行前后执行后的一些操作，pgc登录token存在cookie，saas存在sessionStorage，
# 自带的方法不能取到sessionStorage，用js获取和插入,pgc存在cookie使用自带方法即可
import os.path

import pytest
from Handle.handle_log import log
from playwright.sync_api import Page
from Handle.handle_yaml import domain, token_file, BASE_PATH
from Handle.handle_js import set_cookies


@pytest.fixture(scope='session')
def login_saas_fixture():
    """
    saas执行前登录获取token,pgc使用cookie
    :return:
    """
    pass


# pgc创作者登录获取token
# @pytest.fixture(scope="session")
# def pgc_user_login(
#     browser_type: BrowserType,
#     browser_type_launch_args: Dict,
#     browser_context_args: Dict
# ):
#     # 保存在文件中
#     context = browser_type.launch_persistent_context(Test_File, **{
#         **browser_type_launch_args,
#         **browser_context_args,
#         "locale": "de-DE",
#     }, headless=True)
#     page = context.new_page()
#     page.goto(f'{domain}#/login')
#     LoginPgcUser(page).login_success_user(account_user[0], account_user[1])
#     page.close()
#     context.close()



def pgc_login_user(page:Page):
    # 执行js插入cookie
    page.goto(domain)
    with open(token_file+'_user', 'r', encoding='utf-8') as f:
        cookie = f.read()
        print(cookie)
    # cookies = ['_c_WBKFRo', '_nb_ioWEgULi', 'tfstk', 'vue_pgc_page_rights', 'vue_pgc_role', 'vue_pgc_token']
    cookie = cookie.split(';')
    print(cookie)
    cookie_dict = {}
    for i in cookie:
        key = i.split('=')[0]
        value = i.split('=')[1]
        if key == 'vue_pgc_token':
            cookie_dict = {key: value}
    log.log_info('pgc创作者token'+str(cookie_dict))
    set_cookies(page, cookie_dict)
    yield page
    page.close()


@pytest.fixture()
def browser_context_args(browser_context_args):
    path = os.path.join(BASE_PATH, 'TestCase', 'Case', 'storage.json')
    return {
        **browser_context_args,
        "storage_state" : path
   }





